package com.xing.binbackend.pattern.adapter24.operation;

import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

@Component
public class MonitorAdapterFactory {
    private final Map<String, CloudMonitor> adapterMap;

    // Spring会自动注入所有CloudMonitor实现类
    public MonitorAdapterFactory(List<CloudMonitor> adapters) {
        this.adapterMap = adapters.stream().collect(Collectors.toMap(CloudMonitor::getVendorType,
                Function.identity()));
    }

    public CloudMonitor getAdapter(String vendor) {
        return Optional.ofNullable(adapterMap.get(vendor)).orElseThrow(() -> new IllegalArgumentException("Unsupported cloud vendor"));
    }
}
